package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISubsidyInfoDAO;
import org.jsola.hr.dto.SubsidyInfoAddDTO;
import org.jsola.hr.dto.SubsidyInfoUpdateDTO;
import org.jsola.hr.entity.SubsidyInfoDO;
import org.jsola.hr.query.SubsidyInfoQuery;
import org.jsola.hr.service.ISubsidyInfoService;
import org.jsola.hr.vo.SubsidyInfoListVO;
import org.jsola.hr.vo.SubsidyInfoVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrSubsidyInfoServiceImpl")
public class SubsidyInfoServiceImpl implements ISubsidyInfoService {

    @Autowired
    private ISubsidyInfoDAO subsidyInfoDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SubsidyInfoVO save(SubsidyInfoAddDTO subsidyInfoAddDTO, TokenUser tokenUser) {
        // 转DO
        SubsidyInfoDO subsidyInfoDO = subsidyInfoAddDTO.to(SubsidyInfoDO.class);
        // 保存
        subsidyInfoDO = save(subsidyInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return subsidyInfoDO.to(SubsidyInfoVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SubsidyInfoUpdateDTO subsidyInfoUpdateDTO, TokenUser tokenUser) {
        //转DO
        SubsidyInfoDO subsidyInfoDO = subsidyInfoUpdateDTO.to(SubsidyInfoDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(subsidyInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... subsidyInfoIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) subsidyInfoIds);
    }


    @Override
    public SubsidyInfoVO selectById(String subsidyInfoId, String siteId) {
        SubsidyInfoDO subsidyInfoDO = selectDOById(subsidyInfoId, siteId);
        if (subsidyInfoDO == null) {
            return null;
        }
        return subsidyInfoDO.to(SubsidyInfoVO.class);
    }

    @Override
    public List<SubsidyInfoListVO> select(SubsidyInfoQuery subsidyInfoQuery, String siteId) {
        List<SubsidyInfoDO> subsidyInfoDOList = selectDO(subsidyInfoQuery, siteId);
        if(CollectionUtils.isEmpty(subsidyInfoDOList)) {
            return subsidyInfoDOList == null ? null : new ArrayList<>();
        }
        return subsidyInfoDOList.stream()
                .map(subsidyInfoDO -> subsidyInfoDO.to(SubsidyInfoListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SubsidyInfoQuery subsidyInfoQuery, String siteId) {
        Example example = buildExample(subsidyInfoQuery,siteId);
        return subsidyInfoDAO.selectCountByExample(example);
    }

    @Override
    public Page<SubsidyInfoListVO> selectPage(SubsidyInfoQuery subsidyInfoQuery, String siteId) {
        Example example = buildExample(subsidyInfoQuery,siteId);
        Page<SubsidyInfoDO> page = subsidyInfoDAO.selectPageByExample(example,
                subsidyInfoQuery.getPageNo(),
                subsidyInfoQuery.getPageSize());

        return page.to(SubsidyInfoListVO.class);
    }

    @Override
    public SubsidyInfoDO selectDOById(String subsidyInfoId, String siteId) {
        return listById(subsidyInfoId, siteId);
    }

    @Override
    public List<SubsidyInfoDO> selectDO(SubsidyInfoQuery subsidyInfoQuery, String siteId) {
        Example example = buildExample(subsidyInfoQuery,siteId);
        return subsidyInfoDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param subsidyInfoQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SubsidyInfoQuery subsidyInfoQuery, String siteId) {
        Example example = new Example(SubsidyInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (subsidyInfoQuery.getProgId() != null) {
            example.and().andLike("progId", "%" + subsidyInfoQuery.getProgId() + "%");
        }
        if (subsidyInfoQuery.getName() != null) {
            example.and().andLike("name", "%" + subsidyInfoQuery.getName() + "%");
        }
        if (subsidyInfoQuery.getType() != null) {
            example.and().andEqualTo("type", subsidyInfoQuery.getType());
        }
        if (subsidyInfoQuery.getSendDate() != null) {
            example.and().andEqualTo("sendDate", subsidyInfoQuery.getSendDate());
        }
        if (subsidyInfoQuery.getAmount() != null) {
            example.and().andEqualTo("amount", subsidyInfoQuery.getAmount());
        }
        if (subsidyInfoQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", subsidyInfoQuery.getCompanyId());
        }
        if (subsidyInfoQuery.getGmtCreateStart() != null &&
                subsidyInfoQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", subsidyInfoQuery.getGmtCreateStart(),
                    subsidyInfoQuery.getGmtCreateEnd());
        }
        if (subsidyInfoQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + subsidyInfoQuery.getCreateUserId() + "%");
        }
        if (subsidyInfoQuery.getGmtModifiedStart() != null &&
                subsidyInfoQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", subsidyInfoQuery.getGmtModifiedStart(),
                    subsidyInfoQuery.getGmtModifiedEnd());
        }
        if (subsidyInfoQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + subsidyInfoQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,subsidyInfoQuery.getOrders());
        return example;
    }
}




